package com.cx.update;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;

import com.cx.update.DownloadPrecentListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;

public class FileUtils {
    private String SDCardRoot;

    public FileUtils() {
        this.SDCardRoot = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
    }

    public File createFileInSDCard(String fileName, String dir) throws IOException {
        File file = new File(this.SDCardRoot + dir + File.separator + fileName);
        System.out.println("file---->" + file);
        file.createNewFile();
        return file;
    }

    public File creatSDDir(String dir) {
        File dirFile = new File(this.SDCardRoot + dir + File.separator);
        System.out.println(dirFile.mkdirs());
        return dirFile;
    }

    public boolean isFileExist(String fileName, String path) {
        File file = new File(this.SDCardRoot + path + File.separator + fileName);
        return file.exists();
    }

    public void delete(String fileName, String path) {
        File file = new File(this.SDCardRoot + path + File.separator + fileName);
        if(file.exists()) {
            file.delete();
        }

    }

    public File write2SDFromInput(String path, String fileName, InputStream input) {
        File file = null;
        FileOutputStream output = null;

        try {
            this.creatSDDir(path);
            file = this.createFileInSDCard(fileName, path);
            output = new FileOutputStream(file);
            byte[] e = new byte[4096];

            int temp;
            while((temp = input.read(e)) != -1) {
                output.write(e, 0, temp);
            }

            output.flush();
        } catch (Exception var16) {
            var16.printStackTrace();
        } finally {
            try {
                output.close();
            } catch (Exception var15) {
                var15.printStackTrace();
            }

        }

        return file;
    }

    public int write2SDFromURL(String path, String fileName, String urlStr, DownloadPrecentListener listener) {
        URL url = null;
        InputStream inputStream = null;
        boolean fileSize = false;
        File file = null;
        FileOutputStream output = null;

        try {
            url = new URL(urlStr);
            HttpURLConnection e = (HttpURLConnection)url.openConnection();
            e.setConnectTimeout(15000);
            e.setReadTimeout(20000);
            int var31 = e.getContentLength();
            if(listener != null) {
                listener.startDownload();
            }

            inputStream = e.getInputStream();
            this.creatSDDir(path);
            file = this.createFileInSDCard(fileName, path);
            output = new FileOutputStream(file);
            byte[] buffer = new byte[4096];
            int downLoadedSize = 0;
            int count = 0;

            int temp;
            while((temp = inputStream.read(buffer)) != -1) {
                output.write(buffer, 0, temp);
                downLoadedSize += temp;
                ++count;
                if(listener != null && count >= 10 && var31 != 0) {
                    count = 0;
                    listener.setDownloadedSize(downLoadedSize, var31, (float)(100 * downLoadedSize) / (float)var31);
                }
            }

            if(listener != null) {
                listener.downloadSuccess();
            }

            output.flush();
            return file.length() == (long)var31 && file.length() > 0L?0:-4;
        } catch (MalformedURLException var27) {
            var27.printStackTrace();
        } catch (SocketTimeoutException var28) {
            var28.printStackTrace();
            return -2;
        } catch (IOException var29) {
            var29.printStackTrace();
            return -1;
        } finally {
            try {
                output.close();
                if(inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception var26) {
                var26.printStackTrace();
            }

        }

        return -1;
    }

    public File write2SDFromString(String path, String fileName, String inputString) {
        File file = null;
        FileOutputStream output = null;

        try {
            this.creatSDDir(path);
            file = this.createFileInSDCard(fileName, path);
            output = new FileOutputStream(file);
            byte[] e = inputString.getBytes();
            output.write(e, 0, e.length);
            output.flush();
        } catch (Exception var15) {
            var15.printStackTrace();
        } finally {
            try {
                output.close();
            } catch (Exception var14) {
                var14.printStackTrace();
            }

        }

        return file;
    }
    
    public static synchronized void saveIntData(Context mContext, String valueName, int info) {
        SharedPreferences.Editor editor = mContext.getSharedPreferences("SP_INT_DATA",
                Context.MODE_PRIVATE).edit();
        editor.putInt(valueName, info);
        editor.commit();
    }
    public static synchronized int parseIntData(Context mContext, String valueName) {
        SharedPreferences preferences = mContext.getSharedPreferences("SP_INT_DATA",
                Context.MODE_PRIVATE);
        return preferences.getInt(valueName, -1);
    }
    
    public static void startInstallApp(Context mContext, String path) {
        if (!TextUtils.isEmpty(path) ) {
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setDataAndType(Uri.parse("file://" + path),
                        "application/vnd.android.package-archive");
                mContext.startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
